<template>
  <div class="trainerBox">
    <el-form
      @submit.native.prevent
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="70px"
    >
      <el-form-item label="门店名称" prop="schoolId">
        <el-select v-model="queryParams.schoolId" filterable clearable>
          <el-option label="全部" :value="undefined"></el-option>
          <el-option
            v-for="item in schools"
            :key="item.schoolId"
            :label="item.schoolName"
            :value="item.schoolId"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="员工姓名" prop="nickName">
        <!-- 输入框 -->
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入员工姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="userName">
        <!-- 输入框 -->
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入账号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="岗位名称" prop="stuPostId">
        <el-select v-model="queryParams.stuPostId" filterable clearable>
          <el-option label="全部" :value="undefined"></el-option>
          <el-option
            v-for="item in posts"
            :key="item.postId"
            :label="item.postName"
            :value="item.postId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="抽查名称" prop="title">
        <!-- 输入框 -->
        <el-input
          v-model="queryParams.title"
          placeholder="请输入抽查名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          v-preventReClick
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          >重置</el-button
        >
        <el-button
          v-preventReClick
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      height="330"
      border
      v-loading="loading"
      :data="postList"
      ref="tableRef"
    >
      <el-table-column
        show-overflow-tooltip
        label="门店名称"
        align="center"
        prop="schoolName"
      />

      <el-table-column
        show-overflow-tooltip
        label="员工姓名"
        align="center"
        prop="nickName"
      />
      <el-table-column
        show-overflow-tooltip
        label="账号"
        align="center"
        prop="phoneNumber"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.phoneNumber | regPhone }}</span>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="岗位名称"
        align="center"
        prop="postNames"
      />
      <el-table-column
        show-overflow-tooltip
        label="抽查名称"
        align="center"
        prop="title"
      />
      <el-table-column
        show-overflow-tooltip
        label="成绩"
        align="center"
        prop="score"
      />
      <el-table-column
        show-overflow-tooltip
        label="补考次数"
        align="center"
        prop="applyNum"
      />
      <el-table-column
        show-overflow-tooltip
        label="状态"
        align="center"
        prop="passing"
      >
        <!-- 0未提交 -1不及格-->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.passing == 0" type="info">未提交</el-tag>
          <el-tag v-if="scope.row.passing == -1" type="danger">不及格</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        label="备注"
        align="center"
        prop="remark"
      />
      <!-- <el-table-column
        show-overflow-tooltip
        label="备注"
        align="center"
        prop="remark"
      >
        <template #header>
          备注<el-tooltip class="item" effect="dark" placement="bottom">
            <i
              class="el-icon-question shou"
              style="font-size: 16px; vertical-align: middle; margin-left: 5px"
            ></i>
            <div slot="content">学习超时原因备注</div>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          <div
            @dblclick="handleDoubleClick(scope.row, scope.$index)"
            style="width: 100%; height: 100%"
            v-if="!scope.row.editing"
          >
            {{ scope.row.remark || "无" }}
          </div>
          <el-input
            ref="input_dom"
            v-if="scope.row.editing"
            v-model="scope.row.remark"
            size="small"
            @blur="onRemarkBlur(scope.row, scope.$index, 1)"
          ></el-input>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { editRemark } from "@/api/screen/train";
import { schoolList, postList } from "@/api/screen/courseLearningManagement";
import { failStuList } from "@/api/screen/spotCheck";
export default {
  props: {
    active: [String, Number],
  },
  data() {
    return {
      loading: false,
      postList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolId: undefined,
        nickName: undefined,
        stuPostId: undefined,
        title: undefined,
        userName: undefined,
      },
      total: 10,

      schools: [],
      posts: [],
    };
  },
  mounted() {
    this.getSchoolList();
    this.getPostList();
  },
  watch: {
    active: {
      handler(newVal) {
        if (newVal == 1) {
          this.getList();
        }
      },
      immediate: true,
    },
  },
  methods: {
    async onRemarkBlur(row, index, type) {
      let res = await editRemark({ ...row, remarkType: 2 });
      if (res.code == 200) {
        this.$message.success("备注修改成功");
        this.$set(row, "editing", false);
      }
    },
    handleDoubleClick(row, index) {
      this.$set(row, "editing", true);
      this.$nextTick(() => {
        this.$refs.input_dom.focus();
      });
      // 确保DOM更新后再聚焦
    },
    async getPostList() {
      let res;
      try {
        res = await postList({ pageNum: 1, pageSize: 9999 });
        if (res.code == 200) {
          this.posts = res.rows;
          // 页面数据更新后，让表格滚动条回到顶部
          this.$nextTick(() => {
            if (this.$refs.tableRef) {
              this.$refs.tableRef.bodyWrapper.scrollTop = 0;
            }
          });
        }
      } finally {
      }
    },
    async getSchoolList() {
      let res;
      try {
        res = await schoolList({
          pageNum: 1,
          pageSize: 9999,
        });
        if (res.code == 200) {
          this.schools = res.rows;
        }
      } finally {
      }
    },

    async getList() {
      let res;
      try {
        this.loading = true;

        res = await failStuList(this.queryParams);
        if (res.code == 200) {
          this.postList = res.rows;
          this.total = res.total;
        }
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/screen/spotCheck/failStuExport",
        {
          ...this.queryParams,
        },
        `failStuExport_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
</style>